//------------------------------------------------------------------------------
#include <QtGui>
#include <QSqlRecord>
#include "CColumnModel.h"
#include "CDerniereActionFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CDerniereActionFrm::CDerniereActionFrm(QWidget *parent) : QDialog(parent)
{
	_prospect="";
	
	setupUi(this);
	
	createGrille();
	loadQui();
	
	tableActions->setSortingEnabled(true);
	tableActions->sortByColumn(cndaDate, Qt::DescendingOrder);
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::createGrille(void)
{	
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cndaProspect, "prospect", tr("Prospect"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaNom, "nom", tr("Nom"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaAction, "action", tr("Action"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaDate, "dateAction", tr("Date"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelTime(cndaHeure, "heureAction", tr("Haure"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaQui, "qui", tr("Par"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaInterloc, "interlocuteur", tr("Interlocuteur"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cndaCommentaires, "commentaires", tr("Commentaires"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	
	tableActions->setColumnsModel(columnModel);
	
	tableActions->setWordWrap(true);
	tableActions->setColumnWidth(cndaProspect, 80);
	tableActions->setColumnWidth(cndaDate, 80);
	tableActions->setColumnWidth(cndaHeure, 80);
	tableActions->setColumnWidth(cndaQui, 80);
	tableActions->setColumnWidth(cndaNom, 175);
	tableActions->setColumnWidth(cndaInterloc, 175);
	tableActions->setColumnWidth(cndaCommentaires, 420);
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::loadGrille(void)
{
	QString requete, cond;
	QSqlQuery *query;
	
	cond="WHERE dateAction <= CURDATE() ";
	if(cbQui->itemData(cbQui->currentIndex()).toString() != QString(""))
	{
		cond+="AND qui=\""+cbQui->currentText()+"\" ";
	}
	
	if(ckActGenAuto->checkState() != Qt::Checked)
	{
		cond+="AND auto=0 ";
	}
	
	requete="SELECT prospect, nom, action, dateAction, heureAction, qui, interlocuteur, ap.commentaires, note ";
	requete+="FROM actionsProspect as ap INNER JOIN prospect ON prospect=code ";
	requete+=cond;
	requete+="ORDER BY dateAction DESC LIMIT "+QString::number(NB_ACTION);
	
	clearTable();
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{	
		tableActions->loadGrille(query);
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::loadQui(void)
{
	QString requete;
	QSqlQuery *query;
	int i, sel=0;
	
	cbQui->addItem("Tout le monde", QVariant(""));
	
	requete="SELECT code FROM utilisateur ORDER BY code ";
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		i=1;
		while(query->next())
		{
				QSqlRecord rec=query->record();
				QString code=rec.value("code").toString();
			
				cbQui->addItem(code, QVariant(code));
				if(code == CMySQL::getInstance()->getUser())
				{
					sel=i;
				}
			i++;
		}
	}
	delete query;
	
	cbQui->setCurrentIndex(sel);
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::clearTable(void)
{
	int X, Y;
	for(Y=0;Y<tableActions->rowCount();Y++)
	{
		for(X=0;X<tableActions->getColumnModelCount();X++)
		{
			delete tableActions->item(Y, X);
		}
	}
	//tableActions->clear();
	tableActions->setRowCount(0);
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::on_tableActions_cellDoubleClicked(int row, int col)
{
	_prospect=tableActions->item(row, cndaProspect)->text();
	
	accept();
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::on_cbQui_currentIndexChanged(const QString &text)
{
	loadGrille();
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::on_ckActGenAuto_stateChanged(int state)
{
	loadGrille();
}
//------------------------------------------------------------------------------
void CDerniereActionFrm::on_tableActions_beforeChangeRecord(const QSqlQuery *query, int row) 
{
	QSqlRecord rec=query->record();
	
	QBrush brush=QBrush(rec.value("action").toString() == "" ? QColor(255, 0, 0) : (rec.value("note").toString() == "EXCEL" ? QColor (0, 128, 0) : QColor(0, 0, 0)));
	int X;
	
	for(X=0;X<tableActions->getColumnModelCount();X++)
	{
		tableActions->item(row, X)->setForeground(brush);;
	}
}
//------------------------------------------------------------------------------